const app = getApp()
Page({
  data: {
    detailShow: false,
    statusList: [
      { status: 0, name: '上架' },
      { status: 1, name: '下架' }
    ],
    catalogList: [

    ],
    goods: [

    ],
    goodDetailData: {
      primary_pic_url: '',
      name: '',
      stock_num: '',
      sale_price: '',
      id: ''
    },
    status: 0,
    configState: false,
    configContent: '',
    dialogBodyStyle: {
      width: '70%',
      height: '250rpx'
    },
    classifyName: '',
    classifyViewed: '',
  },
  onLoad: function (options) {
    this.getUserCatalog()
    this.getGoods()

  },
  getGoods() {
    app.Http.http({
      url: app.Api.ProductStock,
      method: 'POST',
      params: {

      }
    }).then(data => {
      let obj = {}
      if (data.length > 0) {
        this.data.catalogList.map((item) => {//分类
          data.map((item1, key1, ary1) => {//商品
            if (item.id === item1.category_id) {//当前分类下的商品
              if (item.subCategoryList.length > 0) {
                item.subCategoryList.map((item2) => {
                  if (item2 != item1.id) {
                    item.subCategoryList.push(item1.id)
                    item.subCategoryList = [...new Set(item.subCategoryList)]
                    obj[item1.id] = item1
                  }
                })
              } else {
                obj[item1.id] = item1
                item.subCategoryList.push(item1.id)
                item.subCategoryList = [...new Set(item.subCategoryList)]
              }
            }
          })
        })
        this.setData({
          catalogList: this.data.catalogList
        })
      } else {
        obj = {}
      }

      this.setData({
        goods: obj
      })
    }).catch(err => {
      console.log(err)
    })
  },
  getUserCatalog() {
    app.Http.http({
      url: app.Api.UserCatalog,
      method: 'POST',
      params: {

      }
    }).then(data => {
      let resultData = data
      if (resultData.length > 0) {
        resultData.map((item) => {
          item.subCategoryList = []
        })
        this.setData({
          catalogList: resultData,
          classifyName: resultData[0].name,
          classifySeleted: 'tab' + resultData[0].id
        })
      } else {
        this.setData({
          catalogList: [],
          classifyName: '',
          classifySeleted: ''
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  tapClassify(e) {
    var id = e.target.dataset.id;
    var name = e.target.dataset.name;
    this.setData({
      classifyViewed: id,
      classifyName: name
    });
    var self = this;
    setTimeout(function () {
      self.setData({
        classifySeleted: id
      });
    }, 100);
  },
  onGoodsScroll: function (e) {
    let catalogList = this.data.catalogList

    if (e.detail.scrollTop > 10 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 10 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }

    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop / scale,
      h = 0,
      classifySeleted,
      len = this.data.catalogList.length;
    this.data.catalogList.forEach(function (classify, i) {
      var _h = 70 + classify.subCategoryList.length * (46 * 3 + 20 * 2);
      if (scrollTop >= h - 100 / scale) {
        classifySeleted = 'tab' + classify.id;
      }
      h += _h;
    });
    this.setData({
      classifySeleted: classifySeleted,
      titleShowState: true
    });
    catalogList.map((item) => {
      if ('tab' + item.id === classifySeleted) {
        this.setData({
          classifyName: item.name
        })
      }
    })
  },
  radioChange(e) {
    let val = e.detail.value
    this.setData({
      status: val
    })
  },
  close() {
    this.setData({
      detailShow: false
    })
  },
  configStateClose() {
    this.setData({
      configState: false
    })
  },
  stockNumInput(e) {
    this.setData({
      "goodDetailData.stock_num": e.detail.value
    })
  },
  salePriceInput(e) {
    this.setData({
      "goodDetailData.sale_price": e.detail.value
    })
  },
  openDetail(e) {
    let item = e.currentTarget.dataset.item
    this.setData({
      detailShow: true,
      status: item.status,
      goodDetailData: {
        primary_pic_url: item.primary_pic_url,
        name: item.name,
        stock_num: item.stock_num,
        status: item.status,
        sale_price: item.sale_price,
        id: item.id
      }
    })
  },
  submit() {
    let saveObj = {}
    if (this.data.goodDetailData.stock_num === '') {
      this.setData({
        configState: true,
        configContent: '请填写库存数量'
      })
      return
    }
    if (this.data.goodDetailData.sale_price === '') {
      this.setData({
        configState: true,
        configContent: '请填写单价'
      })
      return
    }
    saveObj.stock_num = Number(this.data.goodDetailData.stock_num)
    saveObj.sale_price = Number(this.data.goodDetailData.sale_price)
    saveObj.status = Number(this.data.status)
    saveObj.id = this.data.goodDetailData.id
    app.Http.http({
      url: app.Api.ProductUpdate,
      method: 'POST',
      params: saveObj
    }).then(data => {
      this.getUserCatalog()
      this.getGoods()
      this.close()
    }).catch(err => {
      console.log(err)
    })
  },
  onPullDownRefresh: function () {

  },
  changeData(data) {
    this.getUserCatalog()
    this.getGoods()
  }
})